﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace UpdateHost
{
    class Program
    {
        static void updateHosts(bool addNas) {

            FileInfo currentHostsFileInfo = new FileInfo(@"C:\Windows\System32\Drivers\etc\hosts");
            currentHostsFileInfo.CopyTo(@"C:\Windows\System32\Drivers\etc\hosts.bak", true);

            FileStream currentHostsFileStream = currentHostsFileInfo.OpenRead();
            StreamReader currentHostsStreamReader = new StreamReader(currentHostsFileStream);
            String hosts = currentHostsStreamReader.ReadToEnd();
            hosts.Replace("\r\n", "\n");
            currentHostsStreamReader.Close();
            currentHostsFileStream.Close();

            //Console.WriteLine(hosts);
            FileInfo newHostsFileInfo = new FileInfo(@"C:\Windows\System32\Drivers\etc\hosts.new");
            if (newHostsFileInfo.Exists) {
                newHostsFileInfo.Delete();
            }
            FileStream newHostsFileStream = newHostsFileInfo.OpenWrite();
            StreamWriter newHostsStreamWriter = new StreamWriter(newHostsFileStream);

            foreach (String line in Regex.Split(hosts, "\n"))
            {
                if (!"".Equals(line) && !Regex.IsMatch(line, "^192.168.10.110.*$"))
                {
                    newHostsStreamWriter.WriteLine(line);
                    //Console.WriteLine("Copied line : " + line);
                }else{
                    //Console.WriteLine("Skipped line : " + line);
                }
            }
            if (addNas)
            {
                newHostsStreamWriter.WriteLine();
                newHostsStreamWriter.WriteLine("192.168.10.110            ecor6633.dsmynas.org");
                newHostsStreamWriter.WriteLine("192.168.10.110            ecor6633.ecornely.be");
            }
            newHostsStreamWriter.Close();
            newHostsFileStream.Close();

            newHostsFileInfo.CopyTo(@"C:\Windows\System32\Drivers\etc\hosts", true);
            newHostsFileInfo.Delete();
        }

        static bool hasNasReachableIP() {
            NetworkInterface[] ifs = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface net in ifs)
            {
                var addresses = net.GetIPProperties().UnicastAddresses;
                foreach (var address in addresses)
                {
                    if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        if (address.Address.ToString().StartsWith("192.168.10."))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
            ConsoleColor defaultColor = Console.ForegroundColor;
            bool addNas;
            if (args.Length > 0 && args[0] == "-Disconnect")
            {
                addNas = false;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Forced Disconnect");
            }
            else
            {
                addNas = hasNasReachableIP();
            }

            updateHosts(addNas);
            if (addNas)
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("Added nas as 192.168.10.110");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Updated hosts without special nas information");
            }
            Console.ForegroundColor = defaultColor;
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }
    }
}
